home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8963 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.6 KB

  1. Path: armltd.co.uk!dseal
  2. From: dseal@armltd.co.uk (David Seal)
  3. Newsgroups: comp.arch.arithmetic,comp.lang.c,comp.lang.c++
  4. Subject: Re: Access carry flag from C
  5. Date: 27 Feb 1996 17:02:55 GMT
  6. Organization: Advanced RISC Machines Ltd
  7. Message-ID: <4gvdfv$bp7@doc.armltd.co.uk>
  8. References: <Dn1C9z.DGv.0.net@indra.com> <1996Feb1922.17.19.879@koobera.math.uic.edu>         <31298D20.41C6@bazis.nl> <danpop.824859220@rscernix>         <312AFACE.41C6@bazis.nl> <TANMOY.96Feb21081640@qcd.lanl.gov>         <312D8414.167E@bazis.nl> <TANMOY.96Feb23101510@qcd.lanl.gov> <313178D5.41C6@bazis.nl>
  9. NNTP-Posting-Host: sun11.armltd.co.uk
  10.  
  11. Franz Korntner <fkorntne@bazis.nl> writes:
  12.  
  13. >Tanmoy Bhattacharya wrote:
  14. >> It states explicitly that INT_MAX is the maximum value of type int
  15. >> ... how do you read anything else?
  16. >
  17. >As I said, I don't have a copy of the standard at the moment (My 
  18. >company is large, and I cant find the only copy). but the following
  19. >
  20. >>         The values given below shall be replaced by constant expressions
  21. >>         ... Their implementation-defined values shall be greater in magnitude
  22. >>         (absolute value) to those shown, with the same sign.
  23. >
  24. >does imply that the implementation defined values (the plysical limit) shall
  25. >be greater than the shown constants. thus INT_MAX is a minimal limit as the
  26. >implementation defined maximum can (and according to the above- will be) larger
  27. >than the constant. Right ?!?
  28.  
  29. There are three values to consider from the standard:
  30.  
  31. (1) +32767
  32.  
  33. (2) INT_MAX (or to be completely precise, the constant expression that
  34.     INT_MAX is expanded to by the preprocessor).
  35.  
  36. (3) The maximum value that the implementation allows a variable of
  37.     type "int" to take before undefined behaviour occurs.
  38.  
  39. As you say, the standard says that +32767 is the smallest value that
  40. INT_MAX may be - i.e. that value (1) <= value (2). But as Tanmoy
  41. Bhattacharya points out, it *also* says that value (2) is *equal* to
  42. value (3). In short, the requirements imposed by the standard are:
  43.  
  44.   Value (1) < Value (2) = Value (3)
  45.  
  46. and if I read you correctly, you're instead interpreting them as:
  47.  
  48.   Value (1) < Value (2) < Value (3)
  49.  
  50. I.e. while +32767 is indeed a minimum for INT_MAX, this isn't the only
  51. constraint on INT_MAX: it must *also* be equal to the physical limit
  52. your implementation imposes.
  53.  
  54. Having said this, in the case of INT_MAX, there is a let-out clause:
  55. the behaviour of "int" arithmetic above INT_MAX is undefined. That
  56. means that it can be anything you like: it can even be perfectly
  57. standard signed arithmetic up to a higher limit. As a result, it is
  58. legitimate (though perverse) e.g. to produce an implementation in
  59. which "int" arithmetic is done as absolutely standard 32-bit
  60. arithmetic, while you define INT_MAX to be 1048575. What this
  61. effectively means is that you are only guaranteeing "int" arithmetic
  62. up to 1048575, but in fact supplying a version which works up to
  63. 2147483647.
  64.  
  65. The overall result is that INT_MAX + 1, calculated using "int"
  66. arithmetic, must be undefined on every implementation. This doesn't
  67. necessarily mean that it will overflow; it does necessarily mean that
  68. a programmer should not rely on it being correct.
  69.  
  70. In the case of UINT_MAX, the stronger requirements of the standard for
  71. "unsigned" arithmetic lead to somewhat different conclusions:
  72.  
  73. * UINT_MAX is required both to be at least 65535 and to be equal to
  74.   the maximum unsigned value that can be represented.
  75.  
  76. * UINT_MAX + 1, calculated using "unsigned" arithmetic, is required to
  77.   be 0.
  78.  
  79. So the corresponding let-out clause does not exist for unsigned
  80. arithmetic: it is effectively required to be precisely the maximum
  81. implemented unsigned value.
  82.  
  83. David Seal
  84. dseal@armltd.co.uk
  85.